package CutLoad.components
{
	import CutLoad.Base;
	import CutLoad.components.CButton;
	import CutLoad.components.Skin;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * 滚动条 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CScrollBar extends Base
	{
		private var isSetKin:Boolean=false;
		private var barArea:Sprite,btnUp:CButton=null,btnbar:CButton,btnDown:CButton=null;
		//
		private var isBarDown:Boolean=false;
		//移动的速度
		private var numSpeed:Number
		//垂直拖动范围
		private var dragRectH:Rectangle;
		//
		protected var bodyBar:Sprite=new Sprite();
		//
		private var isShowUpDownBtn:Boolean;
		//时刻检测滚动条的值时用到的函数
		private var funchek:Function;
		//
		/**
		 * 
		 * @param numSpeed 上下按钮控制滚动条的移动速度
		 * @param funchek 时刻检测滚动条的值时用到的函数：格式： function(滚动条值)
		 * @param isShowUpDownBtn 是否启用上下按钮
		 * 
		 */		
		public function init(numSpeed:Number=5,funchek:Function=null,isShowUpDownBtn:Boolean=true):void{
			if(funchek==null){
				this.funchek=function(a:Number):void{
				}
			}else{
				this.funchek=funchek;
			}
			var sysKinTem:Array=Skin.getInstance().getSkin(2);
			if(!isSetKin){
				setSkin(sysKinTem,isShowUpDownBtn)
			}
			init_ul(isShowUpDownBtn);
			this.numSpeed=numSpeed;
			//
			this.addChild(bodyBar);
		}
		/**
		 * 直接指定界面元素,不包括上下按钮
		 * @param barArea 滚动条背景
		 * @param btnbar 拖动条
		 * 
		 */		
		public function setSkinUseOther_noUpDonwBtn(barArea:Sprite,btnbar:CButton):void{
			this.barArea=barArea;
			this.btnbar=btnbar
			isSetKin=true;
		}
		/**
		 * 直接指定界面元素,包括上下按钮
		 * @param btnUp 向上按钮
		 * @param barArea 滚动条背景
		 * @param btnbar 拖动条
		 * @param btnDown 向下按钮
		 * 
		 */		
		public function setSkinUseOther(btnUp:CButton,barArea:Sprite,btnbar:CButton,btnDown:CButton):void{
			this.btnUp=btnUp;
			this.barArea=barArea;
			this.btnbar=btnbar;
			this.btnDown=btnDown;	
			isSetKin=true;
		}
		/**
		 * 设置皮肤
		 * @param arrClasname 系统皮肤的类名，数组列名，详见：CutLoad.components.Sking
		 * @param isShowUpDownBtn 是否显示上下按钮
		 * @return 
		 * 
		 */		
		public function setSkin(arrClasname:Array,isShowUpDownBtn:Boolean=true):void{
			isSetKin=true;
			this.isShowUpDownBtn=isShowUpDownBtn;
			if(!isShowUpDownBtn){
				//如果不包括上下按钮
				//背景
				barArea=Base.getStrClassObject(arrClasname[0]) as Sprite
				//拖动条
				btnbar=new CButton();
				if(arrClasname[2].length==3){
					arrClasname[2].push(arrClasname[2][2]);
				}
				btnbar.setSkin(arrClasname[2]);
				btnbar.init();
			}else{
				//拖动条背景
				barArea=Base.getStrClassObject(arrClasname[0]) as Sprite
				//向上按钮
				btnUp=new CButton();
				btnUp.setSkin(arrClasname[1]);
				btnUp.init();
				//拖动条
				btnbar=new CButton();
				if(arrClasname[2].length==3){
					arrClasname[2].push(arrClasname[2][2]);
				}
				btnbar.setSkin(arrClasname[2]);
				btnbar.init();
				//向下按钮
				btnDown=new CButton();
				btnDown.setSkin(arrClasname[3]);
				btnDown.init();
			}
		}
		/**
		 * 隐瞒/显示界面 
		 * @param isOk
		 * 
		 */		
		public function setUiVisible(isOk:Boolean):void{
			barArea.visible=isOk;
			btnbar.visible=isOk;
			btnUp.visible=isOk;
			btnDown.visible=isOk;
		}
		//摆放界面元素及初始化
		protected function init_ul(isShowUpDownBtn:Boolean=true):void{
			if(!isShowUpDownBtn){
				bodyBar.addChild(barArea);
				bodyBar.addChild(btnbar);
				dragRectH=new Rectangle(0,0,0,barArea.height-btnbar.getShowHeight());
				btnbar.addEventListener(MouseEvent.MOUSE_DOWN,barDown);
				//btnbar.addEventListener(MouseEvent.MOUSE_UP,barUp);
				this.addEventListener(Event.ENTER_FRAME,enterFrame);
				return;
			}
			
			btnUp.setOtherInfo({t:1});
			btnbar.y=btnUp.getShowHeight()
			btnDown.y=barArea.height-btnDown.getShowHeight()
			
			btnUp.addEventListener(MouseEvent.MOUSE_DOWN,updown);
			btnDown.addEventListener(MouseEvent.MOUSE_DOWN,updown);
			btnUp.addEventListener(MouseEvent.MOUSE_UP,updown_up);
			btnDown.addEventListener(MouseEvent.MOUSE_UP,updown_up);
			btnDown.addEventListener(MouseEvent.MOUSE_OUT,updown_up);
			btnUp.addEventListener(MouseEvent.MOUSE_OUT,updown_up);
			
			btnDown.setOtherInfo({t:2});
			//
			bodyBar.addChild(barArea);
			bodyBar.addChild(btnUp);
			bodyBar.addChild(btnbar);
			bodyBar.addChild(btnDown);
			//
			dragRectH=new Rectangle(0,btnUp.getShowHeight(),0,barArea.height-btnUp.getShowHeight()-btnbar.getShowHeight()-btnDown.getShowHeight());
			btnbar.addEventListener(MouseEvent.MOUSE_DOWN,barDown);
			//btnbar.addEventListener(MouseEvent.MOUSE_UP,barUp);
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
			//
		}
		/**
		 * 获得滚动条位置 
		 * @return 
		 * 
		 */		
		public function get value():Number{
			if(this.isShowUpDownBtn){
				return (this.btnbar.y-dragRectH.y)/dragRectH.height
			}else{
				return this.btnbar.y/dragRectH.height
			}
		}
		/**
		 * 设置滚动条的位置 
		 * @param a
		 * 
		 */		
		public function set value(a:Number):void{
			if(a<0||a>1) trace("警告:传入的值必须介于 0~1 ! 现在传入的值:"+a);
			if(this.isShowUpDownBtn){
				this.btnbar.y=a*dragRectH.height+dragRectH.y;
			}else{
				this.btnbar.y=a*dragRectH.height
			}
		}
		/**
		 * 滚动条向上或向下 
		 * @param t 格式：只能是:1向上,2向下
		 * @param MultipleSpeed 速度的倍数移动
		 * @param isAddY 是否在滚动y基础上相加。
		 * 
		 */		
		public function moveBar(t:Number=1,MultipleSpeed:Number=1,isAddY:Boolean=true):void{
			if(t<1||t>2) throw new Error("参数只能是:1向上,2向下");
			if(t==1) this.numSpeed=-Math.abs(this.numSpeed);
			if(t==2) this.numSpeed=Math.abs(this.numSpeed);
			var ytem:Number
			if(isAddY){
				ytem=this.btnbar.y+numSpeed*MultipleSpeed;
				if(ytem>=dragRectH.y&&ytem<=(dragRectH.y+dragRectH.height)){
					this.btnbar.y+=numSpeed*MultipleSpeed
				}
			}else{
				ytem=numSpeed*MultipleSpeed;
				if(ytem>=dragRectH.y&&ytem<=(dragRectH.y+dragRectH.height)){
					this.btnbar.y=numSpeed*MultipleSpeed
				}
			}
		}
		//上下按钮按下
		private var isDownBtnUp:Boolean=false,isdownBtnDown:Boolean=false;
		private function updown(e:MouseEvent):void{
			var btn:CButton=e.target as CButton;
			if(btn.getOtherInfo().t==1){
				isDownBtnUp=true;
			}else{
				isdownBtnDown=true;
			}
		}
		//上下按钮松开
		private function updown_up(e:MouseEvent):void{
			var btn:CButton=e.target as CButton;
			if(btn.getOtherInfo().t==1){
				isDownBtnUp=false;
			}else{
				isdownBtnDown=false;
			}
		}
		private function enterFrame(e:Event):void{
			/*if(isBarDown){
				var p:Point=btnbar.localToGlobal(new Point(0,0));
				var xx:Number=stage.mouseX;
				var yy:Number=stage.mouseY;
				if(xx<p.x||xx>(p.x+btnbar.getShowWidth())||yy<p.y||yy>(p.y+btnbar.getShowHeight())){
					isBarDown=false
					btnbar.stopDrag();
				}
			}*/
			if(isDownBtnUp) moveBar(1);
			if(isdownBtnDown) moveBar(2);
			funchek(this.value);
		}
		private function updateDragRect():void{
			//更新滚动条的高度
			if(isShowUpDownBtn){
				dragRectH=new Rectangle(0,btnUp.height,0,barArea.height-btnDown.getShowHeight()-btnUp.getShowHeight()-btnbar.height);
			}else{
				dragRectH=new Rectangle(0,0,0,barArea.height-btnbar.height);
			}
		}
		private function barDown(e:MouseEvent):void{
			isBarDown=true
			btnbar.startDrag(false,dragRectH);
			stage.addEventListener(MouseEvent.MOUSE_UP,barUp);
		}
		private function barUp(e:MouseEvent):void{
			isBarDown=false
			btnbar.stopDrag();
			//
			trace("barUp");
			this.stage.removeEventListener(MouseEvent.MOUSE_UP,barUp)
		} 
		/**
		 * 是否有按住滚动条 
		 * @return 
		 * 
		 */		
		public function getIsDownBar():Boolean{
			return isBarDown;
		}
		/**
		 * 
		 * @param w w不起作用
		 * @param h
		 * 
		 */		
		override public function setWH(w:Number=0, h:Number=0):void{
			barArea.height=h
			if(isShowUpDownBtn) btnDown.y=barArea.height-btnDown.getShowHeight()
			//
			updateDragRect()
		}
		
		override public function killMe():void{
			if(this.stage!=null){
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,barUp)
			}
			super.killMe();
		}
		
	}
	
}